package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


//公共字段的属性填充
@Component
public class MyMetaObject implements MetaObjectHandler {

    //方式1:通过注入的方式来获取session
    //    @Autowired
    //    private HttpServletRequest req;

    @Override
    public void insertFill(MetaObject metaObject) {
//        HttpSession session = req.getSession();
//        Long id = (Long) session.getAttribute("Employee");

        //方式2:通过ThreadLocal携带session中的数据
        Long id = GetSessionId.GetId();
        LocalDateTime time = LocalDateTime.now();
        metaObject.setValue("createTime",time);
        metaObject.setValue("updateTime",time);
        metaObject.setValue("createUser",id);
        metaObject.setValue("updateUser",id);

    }
    @Override
    public void updateFill(MetaObject metaObject) {
//        HttpSession session = req.getSession();
//        Long id = (Long) session.getAttribute("Employee");
        LocalDateTime time = LocalDateTime.now();
        Long id = GetSessionId.GetId();
        metaObject.setValue("updateTime",time);
        metaObject.setValue("updateUser",id);
    }
}
